package com.vhr.service.impl;

import com.vhr.VO.DepartmentCountVO;
import com.vhr.VO.DeptEmpVO;
import com.vhr.VO.MenuVO;
import com.vhr.entities.Department;
import com.vhr.entities.DepartmentExample;
import com.vhr.mapper.DepartmentMapper;
import com.vhr.mapper.EmployeeMapper;
import com.vhr.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class IDepartmentServiceImpl implements IDepartmentService{
    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<DepartmentCountVO> countByDeptId() {
        List<DepartmentCountVO> departmentCountVOList = new ArrayList<DepartmentCountVO>();
        List<Department> depList = getDeptName();
        List<Integer> results = departmentMapper.countByDeptId();
        for(int i=0;i<depList.size();i++){
            DepartmentCountVO departmentCountVO = new DepartmentCountVO(depList.get(i).getDeptName(),results.get(i));
            departmentCountVOList.add(departmentCountVO);
        }
        return departmentCountVOList;
    }
    /*获取部门名称*/
    @Override
    public List<Department> getDeptName() {
        List<Department > deptList = departmentMapper.selectByExample(null);
        return deptList;
    }
    /*获取部门对应员工*/
    @Override
    public List<DeptEmpVO> getEmpByDept() {
        List<Department> depList = getDeptName();
        List<DeptEmpVO> deptEmpVOList = new ArrayList<DeptEmpVO>();

        List<MenuVO> chidren = new ArrayList<MenuVO>();
        List<Department> departmentList = departmentMapper.selectByExample(null);

        for(int i=0;i<depList.size();i++){
            DeptEmpVO deptEmpVO = new DeptEmpVO();
            MenuVO menuVO = new MenuVO();
            chidren = employeeMapper.getEmpByDeptId(i+1);
            deptEmpVO.setEmployees(chidren);
            deptEmpVO.setDeptId(departmentList.get(i).getDeptId());
            deptEmpVO.setDeptName(departmentList.get(i).getDeptName());

            deptEmpVOList.add(deptEmpVO);
        }
        return deptEmpVOList;
    }

//    获取部门信息
    @Override
    public Department selectDepartmentByDeptId(Integer deptId) {
        return departmentMapper.selectByPrimaryKey(deptId);
    }

    @Override
    public Integer selectByDeptName(String deptName) {
        DepartmentExample example = new DepartmentExample();
        example.createCriteria().andDeptNameEqualTo(deptName);
        List<Department> departments = departmentMapper.selectByExample(example);
        return departments.get(0).getDeptId();
    }


}
